/* eslint-disable vue/no-template-shadow */
<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="addDepartment('')"
                    >添加一级部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-tree :data="data" :props="defaultProps" default-expand-all>
          <template slot-scope="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="addDepartment(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="editDepartment(data.id)"
                        >编辑部门</el-dropdown-item>
                        <!--  有下级部门的隐藏删除功能 -->
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="delDepartment(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>

    <!-- 对话框弹窗 -->
    <el-dialog
      :title="isEdit ? '编辑部门' : '添加部门'"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="resetFrom"
    >
      <dept-dialog
        ref="deptDialog"
        :department-id="DepartmentId"
        :is-edit="isEdit"
        :origin-list="originList"
        @flag="showDialog"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import deptDialog from './deptDialog.vue'
export default {
  components: { deptDialog },
  data() {
    return {
      // 存储部门列表
      data: [],
      // 匹配部门名和上下级关系
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      // 开关对话框变量
      dialogFormVisible: false,
      // 存储当前部门的id
      DepartmentId: '',
      // 区别添加和编辑的变量
      isEdit: false,
      // 获取验证需要的相关的数据
      originList: []
    }
  },
  created() {
    this.getdepartments()
  },
  methods: {
    async getdepartments() {
      const { data: res } = await getDepartments()
      res.depts.shift()
      this.data = tranListToTreeData(res.depts)
      res.depts.map((item) => {
        const obj = {
          pid: item.pid,
          name: item.name,
          code: item.code,
          id: item.id
        }
        this.originList.push(obj)
      })
    },
    // 子传父，点击取消按钮关闭对话框
    showDialog() {
      this.dialogFormVisible = false
      // 子组件收到父组件的自定义事件就重新渲染页面
      this.getdepartments()
    },
    // 添加按钮
    addDepartment(id) {
      this.dialogFormVisible = true
      this.DepartmentId = id
      this.isEdit = false
    },
    // 编辑按钮
    editDepartment(id) {
      this.isEdit = true
      this.DepartmentId = id
      this.dialogFormVisible = true

      // 获取子组件的实例对象
      this.$nextTick(() => {
        // 调用子组件的方法
        this.$refs.deptDialog.loadDetail()
      })
    },
    // 删除部门
    delDepartment(id) {
      this.$confirm('您确定要删除吗？', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确认',
        type: 'warning'
      })
        .then(async() => {
          const res = await delDepartment(id)
          this.$message.success(res.message)
          this.getdepartments()
        })
        .catch(() => {})
    },
    // 重置表单
    resetFrom() {
      this.$refs.deptDialog.resetFrom()
    }
  }
}
</script>
